/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * <p>Project: jse2302 - Ex3
 * <p>Powered by webrx On 2023-06-27 14:24:57
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex3 {
    public static Map<String, Integer> map = new HashMap<>();

    public static void main(String[] args) {
        //统计一个目录下有多少种文件
        String path = "D:\\takeaway";

        //count(new File(path));
        count(path);


        System.out.println("--------------");
        Set<String> vs = map.keySet();
        for (String key : vs) {
            System.out.printf("%s文件：%d个%n", key, map.get(key));
        }

    }

    public static void count(String src) {
        count(new File(src));
    }

    public static void count(File src) {
        for (File f : src.listFiles()) {
            if (f.isFile()) {
                String fn = f.getName().toLowerCase();
                String ext = fn;
                if (fn.contains(".")) {
                    //fn = "abc.jpg"  fn.lastIndexOf(".");
                    ext = fn.substring(fn.lastIndexOf("."));
                }

                if (map.containsKey(ext)) {
                    int num = map.get(ext) + 1;
                    map.put(ext, num);
                } else {
                    map.put(ext, 1);
                }

            } else if (f.isDirectory() && !f.getName().equals(".git")) {
                count(f);
            }
        }
    }
}
